//
//  NewCarLoaningViewController.m
//  OneCreditTreasure
//
//  Created by iOS Tedu on 16/8/26.
//
//

#import "NewCarLoaningViewController.h"
#import "DownPaymentView.h"
#import "DetailInfoTableViewCell.h"
#import "InputCarPricesTableViewCell.h"
#import "paymentTableViewCell.h"

@interface NewCarLoaningViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;

@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, strong) NSArray *demoDataArr;

@property (nonatomic, getter=isShowing) BOOL showAll;
@end

@implementation NewCarLoaningViewController
#pragma mark - UITextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - UITableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.isShowing) {
        return section == 3 ? self.dataArr.count : (section == 0 ? self.demoDataArr.count : 1);
    }
    return section == 3 ? self.dataArr.count : 1;
}

kRemobeCellSeparator
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 3) {
        DetailInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailCell" forIndexPath:indexPath];
        cell.textLabel.text = self.dataArr[indexPath.row];
        cell.infoLB.text = @"123456";
        return cell;
    } else if (indexPath.section == 2) {
        paymentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"paymentCell" forIndexPath:indexPath];
        return cell;
    } else if (indexPath.section == 1) {
        InputCarPricesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"inputCell" forIndexPath:indexPath];
        cell.inputTF.delegate = self;
        return cell;
    } else {
        UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        Cell.textLabel.text = self.demoDataArr[indexPath.row];
        Cell.textLabel.textAlignment = NSTextAlignmentCenter;
        Cell.selectionStyle = UITableViewCellSelectionStyleGray;
        Cell.textLabel.font = defaultFont;
        Cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return Cell;
    }
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        self.showAll = !self.isShowing;
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 2 ? 250 * AUTO_WIDTH : 44;
}

#pragma mark - 生命周期 life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init {
    if (self = [super init]) {
        self.title = @"保时捷-Boxster";
        self.showAll = NO;
    }
    return self;
}

#pragma mark - 懒加载 lazy loading
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(0);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        [_tableView registerClass:[InputCarPricesTableViewCell class] forCellReuseIdentifier:@"inputCell"];
        [_tableView registerClass:[DetailInfoTableViewCell class] forCellReuseIdentifier:@"detailCell"];
        [_tableView registerClass:[paymentTableViewCell class] forCellReuseIdentifier:@"paymentCell"];
        
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
    }
    return _tableView;
}

- (NSArray *)dataArr {
    if(_dataArr == nil) {
        _dataArr = @[@"贷款额度 (元)",
                     @"保证金 (元)",
                     @"还款方式",
                     @"月供 (元)",
                     @"月利率",
                     @"总利息 (元)"];
    }
    return _dataArr;
}

- (NSArray *)demoDataArr {
    if(_demoDataArr == nil) {
        _demoDataArr = @[@"2015款 BoxsterStyleEdition2.7L",
                         @"2015款 BoxsterStyleEdition2.7L",
                         @"2015款 BoxsterStyleEdition2.7L",
                         @"2015款 BoxsterStyleEdition2.7L",
                         @"2015款 BoxsterStyleEdition2.7L",
                         @"2015款 BoxsterStyleEdition2.7L",
                         @"2015款 BoxsterStyleEdition2.7L",
                         @"2015款 BoxsterStyleEdition2.7L"];
    }
    return _demoDataArr;
}

- (UIView *)headerView {
    if(_headerView == nil) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 150)];
        
        UIImageView *headerIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"11113"]];
        [_headerView addSubview:headerIV];
        [headerIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(0);
        }];
    }
    return _headerView;
}

- (UIView *)footerView {
    if(_footerView == nil) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 44)];
        
        CGFloat collectionBtnWidth = kScreenW / 4;
        UIButton *collectBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_footerView addSubview:collectBtn];
        [collectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(0);
            make.width.equalTo(collectionBtnWidth);
        }];
        [collectBtn setTitle:@"收藏" forState:UIControlStateNormal];
        myBtnSettings(1, [[UIColor lightGrayColor] CGColor], [UIColor blackColor], 0, collectBtn);
        
        UIButton *collectionFloderBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_footerView addSubview:collectionFloderBtn];
        [collectionFloderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(collectBtn.mas_right).equalTo(-1);
            make.top.bottom.equalTo(0);
            make.width.equalTo(collectionBtnWidth);
        }];
        [collectionFloderBtn setTitle:@"收藏夹" forState:UIControlStateNormal];
        myBtnSettings(1, [[UIColor lightGrayColor] CGColor], [UIColor blackColor], 0, collectionFloderBtn);
        
        UIButton *applyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_footerView addSubview:applyBtn];
        CGFloat applyBtnWidth = collectionBtnWidth * 2;
        [applyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(collectionFloderBtn.mas_right).equalTo(-1);
            make.top.bottom.equalTo(0);
            make.width.equalTo(applyBtnWidth);
        }];
        [applyBtn setTitle:@"立即申请" forState:UIControlStateNormal];
        myBtnSettings(1, [[UIColor lightGrayColor] CGColor], [UIColor blackColor], 0, applyBtn);
        
    }
    return _footerView;
}

@end
